import requests
import json
from datetime import datetime
from AutoRegister.settings import BASE_URL, GET_TOKEN_URL, TEAM_URL


def getAccessToken():
    """
    获取秘钥
    """
    url = BASE_URL + GET_TOKEN_URL
    AccessToken = requests.get(url).json()['access_token']
    return AccessToken


def sendTeam(message, types):
    AccessToken = getAccessToken()
    msg = {'chatid': types, 'msgtype': 'text', 'text': {'content': message}}
    url = BASE_URL + TEAM_URL.format(AccessToken)
    res_status = requests.post(url, json.dumps(msg)).json()['errmsg']
    if res_status == 'ok':
        return '发送成功'
    else:
        print('发送失败')
        return res_status
